<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class article extends core {
	private $article_mod;

	public function __construct() {
		parent::__construct();
		$this->article_mod = m("article");
	}

	//发现首页
	public function index() {
		$where = "";
		$type_id = (isset($_GET['type_id']) && trim($_GET['type_id'])) ? trim($_GET['type_id']) : '';
		//关键词搜索
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';		
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		if ($keyword) {
			$where .=" AND (title like '%{$keyword}%' OR content like '%{$keyword}%') ";
		}
		if ($type_id) {
			$where .=" AND type_id='{$type_id}' ";
		}
		$sql = "SELECT * FROM jf_article WHERE status=1 AND id>'10' {$where} ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";
		$articles = $this->db->get_results($sql);
		if ($articles) {
			foreach ($articles as $k=>$article) {
				/*$sql = "SELECT pic FROM jf_article_pic WHERE article_id='{$article->id}' ORDER BY id ASC LIMIT 3";
				$pics = $this->db->get_results($sql);
				$articles[$k]->pics = $pics;
				$sql = "SELECT goods_id FROM jf_article_goods WHERE article_id='{$article->id}' ORDER BY id ASC";
				$goods = $this->db->get_results($sql);
				$articles[$k]->goods = $goods;*/
				$articles[$k]->add_time = get_time_word($article->add_time);
			}
		}
		
		$flashes = $this->_flash();
		success(array("flashes" => $flashes, "articles" => $articles));
	}
	
	//添加/修改
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			if ($id) {
				$sql = "SELECT * FROM jf_article WHERE member_id='{$this->member_id}' AND id='{$id}'";
				$rs = $this->db->get_row($sql);
				success($rs);
			}
			success('ok');
		}
	}
	
	//保存
	public function save() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$title = isset($_POST['title']) ? trim($_POST['title']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$pics = isset($_POST['pics']) ? stripslashes($_POST['pics']) : '';
		$goods = isset($_POST['goods']) ? stripslashes($_POST['goods']) : '';
		if (!$title || !$content || !$pics) error('缺少资料');
		
		if ($id) {
			$sql = "UPDATE jf_article SET title='{$title}', content='{$content}' 
				WHERE member_id='{$this->member_id}' AND id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO jf_article (title, content, member_id, add_time) 
				VALUES ('{$title}', '{$content}', '{$this->member_id}', '".time()."')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		
		//保存图片
		if ($pics) {
			$sql = "DELETE FROM jf_article_pic WHERE article_id='{$id}'";
			$this->db->query($sql);
			$pics = json_decode($pics);
			foreach ($pics as $p) {
				$sql = "INSERT INTO jf_article_pic (article_id, pic, add_time) VALUES ('{$id}', '{$p}', '".time()."')";
				$this->db->query($sql);
			}
		}
		
		//保存关联产品
		if ($goods) {
			$sql = "DELETE FROM jf_article_goods WHERE article_id='{$id}'";
			$this->db->query($sql);
			$goods = json_decode($goods);
			foreach ($goods as $g) {
				$sql = "INSERT INTO jf_article_goods (article_id, goods_id) VALUES ('{$id}', '{$g}')";
				$this->db->query($sql);
			}
		}
		
		$sql = "SELECT * FROM jf_article WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$rs = $this->db->get_row($sql);
		success($rs);
	}

	//轮播图
	private function _flash() {
		$sql = "SELECT * FROM jf_ad WHERE status=1 AND position='faxian' ORDER BY sort ASC, id DESC LIMIT 5";
		//echo $sql;
		$flashes = $this->db->get_results($sql);
		if ($flashes) {
			$flashes = add_domain_deep($flashes, array("pic"));
		}
		return $flashes;
	}

	//文章详情
	public function detail() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$row = $this->article_mod->detail($id);
		if ($row) {
			$row->content = stripslashes($row->content);
			$row->add_time = date("Y-m-d H:i:s", $row->add_time);
			/*$row->liked = $this->_liked($id); //是否点赞
			//$sql = "SELECT count(*) FROM jf_article_comment WHERE article_id='{$id}'";
			$row->likes_list = $this->likes($id);
			$row->comments_list = $this->_comments($id, $offset, $pagesize);
			$row->goods = $this->_goods($id);*/
			$sql = "UPDATE jf_article SET clicks=clicks+1 WHERE id='{$id}'";
			$this->db->query($sql);
		}
		success($row);
	}

	//关联商品
	private function _goods($article_id) {
		$sql = "SELECT g.id, g.name, g.model, g.default_pic, g.price FROM jf_article_goods ag LEFT JOIN jf_goods g ON goods_id=g.id 
			WHERE article_id='{$article_id}' ORDER BY ag.id ASC";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	//文章的点赞
	public function likes($article_id) {
		$sql = "SELECT m.id as member_id, m.avatar, m.nick_name 
			FROM jf_article_like l LEFT JOIN jf_member m ON l.member_id=m.id 
			WHERE l.article_id='{$article_id}' ORDER BY l.id DESC";
		$likes = $this->db->get_results($sql);
		return $likes;
	}

	//点赞
	public function like() {
		$article_id = (isset($_POST['article_id']) && trim($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		if ($this->member_id && $article_id) {
			$sql = "SELECT count(*) FROM jf_article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
			$exists = intval($this->db->get_var($sql));
			if ($exists) {
				$sql = "DELETE FROM jf_article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
				$this->db->query($sql);
			} else {
				$sql = "INSERT INTO jf_article_like(article_id, member_id, add_time, ip) 
					VALUES('{$article_id}', '{$this->member_id}', '".time()."', '".ip()."')";
				$this->db->query($sql);
			}
			$sql = "UPDATE jf_article SET likes=(SELECT count(*) FROM jf_article_like WHERE article_id='{$article_id}') WHERE id='{$article_id}'";
			$this->db->query($sql);	
		}
		success("ok");
	}

	//发表评论
	public function post_comment() {
		$article_id = (isset($_POST['article_id']) && trim($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$sql = "INSERT INTO jf_article_comment(article_id, member_id, content, add_time, ip) 
				VALUES('{$article_id}', '{$this->member_id}', '{$content}', '".time()."', '".ip()."')";
		$this->db->query($sql);	
		$sql = "UPDATE jf_article SET comments=(SELECT count(*) FROM jf_article_comment WHERE article_id='{$article_id}') WHERE id='{$article_id}'";
		$this->db->query($sql);		
		success($article_id);
	}

	//列出评论
	public function comments() {
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;	
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$comments = $this->_comments($article_id, $offset, $pagesize);
		success($comments);
	}

	//列出评论
	public function _comments($article_id, $offset, $pagesize) {
		$sql = "SELECT c.*, m.id as member_id, m.avatar, m.name as member_name ,m.nick_name as member_nick_name  
			FROM jf_article_comment c LEFT JOIN jf_member m ON c.member_id=m.id 
			WHERE article_id='{$article_id}' ORDER BY c.id DESC LIMIT {$offset}, {$pagesize}";
		//echo $sql;
		$comments = $this->db->get_results($sql);
		if ($comments) {
			$comments = add_domain_deep($comments, array("avatar"));
			foreach ($comments as $k=>$comment) {
				$comments[$k]->add_time = get_time_word($comment->add_time);
				if(preg_match("/^1[34578]\d{9}$/", $comment->member_name)){
					$comment->member_name = substr_replace($comment->member_name,'*****',3,-3);
				}
			}
		}
		return $comments;
	}

	//是否点赞
	private function _liked($article_id) {
		$sql = "SELECT count(*) FROM jf_article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
		return intval($this->db->get_var($sql));
	}

}
